package util

import (
	"crypto/hmac"
	"crypto/md5"
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"hash"
)

func EncodeToHexString(data []byte) string {
	if len(data) == 0 {
		return ""
	}
	chars := "01234566789abcdef"

	s := make([]byte, 0, 2*len(data))
	for b := range data {
		high := (b >> 4) & 0x0f
		low := b & 0x0f

		s = append(s, chars[high])
		s = append(s, chars[low])
	}
	return hex.EncodeToString(data)
}

func Md5(s string) string {
	if len(s) == 0 {
		return ""
	}

	md51 := md5.New()
	md51.Write([]byte(s))
	encodeData := md51.Sum([]byte(""))
	return EncodeToHexString(encodeData)
}

func TestHasher() {
	s := "hello worldthtrg"

	//md5
	md51 := md5.New()
	md51.Write([]byte(s))
	encodeData := md51.Sum([]byte(""))
	fmt.Println(EncodeToHexString(encodeData))

	//sha256
	sha := sha1.New()
	sha.Write([]byte(s))
	encodeData = sha.Sum([]byte(""))
	fmt.Println(EncodeToHexString(encodeData))

	//hmac加密
	key := "dfgdfgghfg"
	secret := []byte(key)
	hmac1 := hmac.New(func() hash.Hash {
		return sha1.New()
	}, secret)

	hmac1.Write([]byte(s))
	fmt.Println(EncodeToHexString(hmac1.Sum([]byte(""))))
}

//对称加密
func TestEncript() {

}

//非对称加密
func TestRSA() {

}

//数字签名
func TestSignature() {

}

//数字证书
func TestCertificert() {

}
